package com.jimikongjian.controller;

import com.jimikongjian.models.Like;
import com.jimikongjian.models.ResponseMessage;
import com.jimikongjian.service.base.ILikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by fuxiuyang on 17/3/14.
 */
@RestController("likeController")
@RequestMapping(value = "/v1")
public class LikeController {


    @Autowired
    @Qualifier("likeService")
    private ILikeService likeService;

    /**
     * 收藏一篇文章
     * @param like
     * @return
     */
    @RequestMapping(value = "/like", method = RequestMethod.POST)
    @ResponseBody
    public ResponseMessage likeArticle(@RequestBody Like like){
        return likeService.likeArticle(like);
    }
    /**
     * 查询用户收藏的情况
     * @param userId
     * @param articleId
     * @return
     */
    @RequestMapping(value = "/like", method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage selectLike(@RequestParam(name = "userId", defaultValue = "-1") Integer userId,
                                      @RequestParam("articleId") Integer articleId){
        if (articleId != null){
            return likeService.selectLikeByUserIdAndArticleId(userId, articleId);
        }
        return likeService.selectLikeByUserId(userId);
    }


    /**
     * 取消某一个收藏
     * @param like
     * @return
     */
    @RequestMapping(value = "/like", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseMessage deleteLike(@RequestBody Like like){
        return likeService.deleteLike(like);
    }
}
